<!-- 直接打开方式-ueditor -->
<template>
  <div class="mod-demo-ueditor">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="文档类型" prop="typeId">
            <el-select v-model="dataForm.typeId" filterable placeholder="请选择文档分类" size="medium" @change="onSelectType">
              <el-option v-for="item in documentCatList" :key="item.typeId" :label="item.name" :value="item.typeId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否置顶" prop="showtop">
            <el-radio-group v-model="dataForm.showtop">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="文关键词" prop="keyword">
            <el-input v-model="dataForm.keyword" placeholder="关键词" style="width:300px;" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="文档标题" prop="title">
            <el-input v-model="dataForm.title" placeholder="标题" style="width:300px;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row>-->
<!--        <el-col :span="15">-->
<!--          <el-form-item label="文档图片" prop="picture">-->
<!--            <div class="myhead">-->
<!--              <el-tooltip content="点击上传查看图" placement="top">-->
<!--                <el-image  style="width: 90px; height: 60px" :src="dataForm.picture" fit="contain" @click="uploadImageHandle()" alt=""/>-->
<!--              </el-tooltip>-->
<!--            </div>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-row>
        <el-col>
          <el-form-item label="文档内容" prop="content">
            <vue-ueditor-wrap
              v-model="dataForm.content"
              :config="myConfig"
              :destroy="true"
              @ready="ready"
              mode="observer"
              :observerDebounceTime="100"
              :observerOptions="{ attributes: true, characterData: true, childList: true, subtree: true }">
            </vue-ueditor-wrap>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer" style="text-align: center;display:block;">
      <el-button @click="dataFormCancel()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗, 上传缩略图 -->
    <upload-image v-if="uploadImageVisible" ref="uploadImage" @refreshDataList="refreshUploadImage"></upload-image>
  </div>
</template>

<script>
  // 1、引入VueUeditorWrap组件
  import VueUeditorWrap from 'vue-ueditor-wrap' // ES6 Module
  import UploadImage from './upload-image-common'
  export default {
    name: 'HelloWorld',
    // 2、注册组件
    components: {
      UploadImage,
      VueUeditorWrap
    },
    data () {
      return {
          myConfig: {
            // 编辑器不自动被内容撑高
            autoHeightEnabled: false,
            // 初始容器高度
            initialFrameHeight: 500,
            // 初始容器宽度
            initialFrameWidth: '100%',
            // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
            //serverUrl: 'http://35.201.165.105:8000/controller.php',
            serverUrl: window.SITE_CONFIG.baseUrl + '/ueditor', // domain+/ueditor/controller.php
            // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
            UEDITOR_HOME_URL: 'static/plugins/UEditor/',
          },
        documentCatList: [
          {
            "id": 1,
            "name": "系统介绍",
          }, {
            "id": 2,
            "name": "关于我们",
          }, {
            "id": 3,
            "name": "系统教程",
          }, {
            "id": 4,
            "name": "隐私政策",
          }, {
            "id": 5,
            "name": "服务条款",
          }, {
            "id": 6,
            "name": "最新资讯",
          }, {
            "id": 7,
            "name": "系统帮助",
          }
        ],
        uploadImageVisible: false,
        dataForm: {
          id: 0,
          typeId: '',
          typeName: '',
          title: '',
          keyword: '',
          content: '<h2>请再此输入文档内容</h2>',
          picture: 'http://file-cdn-theme.cykonhy.com/default/default.jpg',
          showtop: 1,
          clickcount: '250',
          remark: ''
        },
        dataRule: {
          typeId: [
            { required: true, message: '类型id不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          keyword: [
            { required: true, message: '关键词不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          picture: [
            { required: true, message: '图片URL不能为空', trigger: 'blur' }
          ],
          showtop: [
            { required: true, message: '是否置顶不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    activated () {
      let id = this.$route.query.id
      if(id != null){
        this.init(id)
      }else {
        this.dataForm.id = 0
        this.dataForm.typeId = '',
          this.dataForm.typeName = '',
          this.dataForm.title ='',
          this.dataForm.keyword = '',
          this.dataForm.content = '<h2>请再此输入文档内容</h2>',
          this.dataForm.picture =  'http://file-cdn-theme.cykonhy.com/default/default.jpg',
          this.dataForm.showtop = 1,
          this.dataForm.clickcount = '250',
          this.dataForm.remark = ''
      }
    },
    methods: {
      back () {
        window.history.back()
      },
      ready (editorInstance) {
        console.log(`实例${editorInstance.key}已经初始化:`, editorInstance)
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/document/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.document.id
                this.dataForm.typeId = data.document.typeId
                this.dataForm.typeName = data.document.typeName
                this.dataForm.title = data.document.title
                this.dataForm.keyword = data.document.keyword
                this.dataForm.content = data.document.content
                this.dataForm.picture = data.document.picture
                this.dataForm.clickcount = data.document.clickcount
                this.dataForm.showtop = data.document.showtop
                this.dataForm.remark = data.document.remark
              }
            })
          }
        })
      },
      // 筛选类型
      onSelectType (val) {
        let documentType = null ;
        this.documentCatList.forEach((item, index) => {
          if (item.typeId === val){
            documentType = item
          }
        })
        this.dataForm.typeName = documentType.name
      },
      // 上传图片
      uploadImageHandle () {
        this.uploadImageVisible = true
        this.$nextTick(() => {
          this.$refs.uploadImage.init()
        })
      },
      refreshUploadImage (filePath) {
        if (filePath != ""){
          this.dataForm.picture = filePath
        }
        this.uploadImageVisible = false
      },
      // 表单提交
      dataFormSubmit () {
        console.log(this.dataForm.id)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/document/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'typeId': this.dataForm.typeId,
                'typeName': this.dataForm.typeName,
                'title': this.dataForm.title,
                'keyword': this.dataForm.keyword,
                'content': this.dataForm.content,
                'picture': this.dataForm.picture,
                'clickcount': this.dataForm.clickcount,
                'showtop': this.dataForm.showtop,
                'remark': this.dataForm.remark,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$router.push({ path: `sys-document`})
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      dataFormCancel(){
        this.visible = false
        this.$router.push({ path: `sys-document`})
      }
    }
  }
</script>

<style lang="scss">
  .mod-demo-ueditor {
    position: relative;
    z-index: 510;
    > .el-alert {
      margin-bottom: 10px;
    }
  }
</style>
